<?php
/**
 * Class that operate on table 'articles_author'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class ArticlesAuthorMySqlDAO implements ArticlesAuthorDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ArticlesAuthorMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM articles_author WHERE author_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM articles_author';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM articles_author ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param articlesAuthor primary key
 	 */
	public function delete($author_id){
		$sql = 'DELETE FROM articles_author WHERE author_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($author_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param ArticlesAuthorMySql articlesAuthor
 	 */
	public function insert($articlesAuthor){
		$sql = 'INSERT INTO articles_author (title, first_name, last_name, middle_name, address1, address2, city, state, country, zip, phone1, phone2, email, description, created_by, created_date, updated_date, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($articlesAuthor->title);
		$sqlQuery->set($articlesAuthor->firstName);
		$sqlQuery->set($articlesAuthor->lastName);
		$sqlQuery->set($articlesAuthor->middleName);
		$sqlQuery->set($articlesAuthor->address1);
		$sqlQuery->set($articlesAuthor->address2);
		$sqlQuery->set($articlesAuthor->city);
		$sqlQuery->set($articlesAuthor->state);
		$sqlQuery->set($articlesAuthor->country);
		$sqlQuery->set($articlesAuthor->zip);
		$sqlQuery->set($articlesAuthor->phone1);
		$sqlQuery->set($articlesAuthor->phone2);
		$sqlQuery->set($articlesAuthor->email);
		$sqlQuery->set($articlesAuthor->description);
		$sqlQuery->setNumber($articlesAuthor->createdBy);
		$sqlQuery->set($articlesAuthor->created_date);
		$sqlQuery->set($articlesAuthor->updated_date);
		$sqlQuery->setNumber($articlesAuthor->status);

		$id = $this->executeInsert($sqlQuery);	
		$articlesAuthor->authorId = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param ArticlesAuthorMySql articlesAuthor
 	 */
	public function update($articlesAuthor){
		$sql = 'UPDATE articles_author SET title = ?, first_name = ?, last_name = ?, middle_name = ?, address1 = ?, address2 = ?, city = ?, state = ?, country = ?, zip = ?, phone1 = ?, phone2 = ?, email = ?, description = ?, created_by = ?, created_date = ?, updated_date = ?, status = ? WHERE author_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($articlesAuthor->title);
		$sqlQuery->set($articlesAuthor->firstName);
		$sqlQuery->set($articlesAuthor->lastName);
		$sqlQuery->set($articlesAuthor->middleName);
		$sqlQuery->set($articlesAuthor->address1);
		$sqlQuery->set($articlesAuthor->address2);
		$sqlQuery->set($articlesAuthor->city);
		$sqlQuery->set($articlesAuthor->state);
		$sqlQuery->set($articlesAuthor->country);
		$sqlQuery->set($articlesAuthor->zip);
		$sqlQuery->set($articlesAuthor->phone1);
		$sqlQuery->set($articlesAuthor->phone2);
		$sqlQuery->set($articlesAuthor->email);
		$sqlQuery->set($articlesAuthor->description);
		$sqlQuery->setNumber($articlesAuthor->createdBy);
		$sqlQuery->set($articlesAuthor->created_date);
		$sqlQuery->set($articlesAuthor->updated_date);
		$sqlQuery->setNumber($articlesAuthor->status);

		$sqlQuery->setNumber($articlesAuthor->authorId);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM articles_author';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByTitle($value){
		$sql = 'SELECT * FROM articles_author WHERE title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFirstName($value){
		$sql = 'SELECT * FROM articles_author WHERE first_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLastName($value){
		$sql = 'SELECT * FROM articles_author WHERE last_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMiddleName($value){
		$sql = 'SELECT * FROM articles_author WHERE middle_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAddress1($value){
		$sql = 'SELECT * FROM articles_author WHERE address1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAddress2($value){
		$sql = 'SELECT * FROM articles_author WHERE address2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCity($value){
		$sql = 'SELECT * FROM articles_author WHERE city = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByState($value){
		$sql = 'SELECT * FROM articles_author WHERE state = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCountry($value){
		$sql = 'SELECT * FROM articles_author WHERE country = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByZip($value){
		$sql = 'SELECT * FROM articles_author WHERE zip = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPhone1($value){
		$sql = 'SELECT * FROM articles_author WHERE phone1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPhone2($value){
		$sql = 'SELECT * FROM articles_author WHERE phone2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEmail($value){
		$sql = 'SELECT * FROM articles_author WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescription($value){
		$sql = 'SELECT * FROM articles_author WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedBy($value){
		$sql = 'SELECT * FROM articles_author WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM articles_author WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM articles_author WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM articles_author WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByTitle($value){
		$sql = 'DELETE FROM articles_author WHERE title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFirstName($value){
		$sql = 'DELETE FROM articles_author WHERE first_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLastName($value){
		$sql = 'DELETE FROM articles_author WHERE last_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMiddleName($value){
		$sql = 'DELETE FROM articles_author WHERE middle_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAddress1($value){
		$sql = 'DELETE FROM articles_author WHERE address1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAddress2($value){
		$sql = 'DELETE FROM articles_author WHERE address2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCity($value){
		$sql = 'DELETE FROM articles_author WHERE city = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByState($value){
		$sql = 'DELETE FROM articles_author WHERE state = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCountry($value){
		$sql = 'DELETE FROM articles_author WHERE country = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByZip($value){
		$sql = 'DELETE FROM articles_author WHERE zip = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPhone1($value){
		$sql = 'DELETE FROM articles_author WHERE phone1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPhone2($value){
		$sql = 'DELETE FROM articles_author WHERE phone2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEmail($value){
		$sql = 'DELETE FROM articles_author WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescription($value){
		$sql = 'DELETE FROM articles_author WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedBy($value){
		$sql = 'DELETE FROM articles_author WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM articles_author WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM articles_author WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM articles_author WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return ArticlesAuthorMySql 
	 */
	protected function readRow($row){
		$articlesAuthor = new ArticlesAuthor();
		
		$articlesAuthor->authorId = $row['author_id'];
		$articlesAuthor->title = $row['title'];
		$articlesAuthor->firstName = $row['first_name'];
		$articlesAuthor->lastName = $row['last_name'];
		$articlesAuthor->middleName = $row['middle_name'];
		$articlesAuthor->address1 = $row['address1'];
		$articlesAuthor->address2 = $row['address2'];
		$articlesAuthor->city = $row['city'];
		$articlesAuthor->state = $row['state'];
		$articlesAuthor->country = $row['country'];
		$articlesAuthor->zip = $row['zip'];
		$articlesAuthor->phone1 = $row['phone1'];
		$articlesAuthor->phone2 = $row['phone2'];
		$articlesAuthor->email = $row['email'];
		$articlesAuthor->description = $row['description'];
		$articlesAuthor->createdBy = $row['created_by'];
		$articlesAuthor->created_date = $row['created_date'];
		$articlesAuthor->updated_date = $row['updated_date'];
		$articlesAuthor->status = $row['status'];

		return $articlesAuthor;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ArticlesAuthorMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>